import { app, BrowserWindow,Menu,MenuItem } from 'electron';
import is from 'electron-is';
import { join } from 'path';
import log from 'electron-log';

app.setName('设计大师');

import * as window from './services/window';

function application_init() {
  const win = window.create({ width: 1000, height: 600 ,webPreferences: {
    webSecurity: false
}});
  win.loadURL(window.getPath());
  win.openDevTools();
}


// import * as menu from './services/menu';
function menu_init(){
  const menu = Menu.buildFromTemplate([
    {
      label: '信管平台',
      submenu: [
        { label:'关于' },
        { label:'帮助' },
      ],
    },
    {

  },
  {
    label: '采购',
    submenu: [
      { label:'销售合同'  , click: (menuItem,browserWindow) => {browserWindow.loadURL(window.getPath()+'#/contract')} },
      { label:'采购单'  , click: (menuItem,browserWindow) => {browserWindow.loadURL(window.getPath()+'#/purchase')} },
    ],
  },
  {
    label: '货运物流',
    submenu: [
      { label:'提箱单'  , click: (menuItem,browserWindow) => {browserWindow.loadURL(window.getPath()+'#/container')} },
    ],
  },
  {
    label: '产品库',
    submenu: [
      { label:'SKU商品'  , click: (menuItem,browserWindow) => {browserWindow.loadURL(window.getPath()+'#/product')} },
      { label:'采集商品'  , click: (menuItem,browserWindow) => {browserWindow.loadURL(window.getPath()+'#/gather')} },
    ],
  },
  {
    label: '账户',
    submenu: [
      { label:'我的信息'  , click: (menuItem,browserWindow) => {browserWindow.loadURL(window.getPath()+'#/account')} },
      { label:'我的消息'  , click: (menuItem,browserWindow) => {browserWindow.loadURL(window.getPath()+'#/message')} },
      { label:'注销' , click: (menuItem,browserWindow) => {browserWindow.loadURL(window.getPath()+'#/logout')} },
    ],
  },
  ]);
  Menu.setApplicationMenu(menu);
}

import Config from 'electron-config';

const config = new Config({ name: 'config' });

log.transports.file.level = 'info';

log.info('(main/index) >>>>>>>>>>>>>>>>>>');
log.info('(main/index) app start');
log.info(`(main/index) log file at ${log.findLogPath()}`);

if (is.dev()) {
  require('electron-debug')();
}

app.on('ready', () => {
  log.info('(main/index) app ready');
  application_init();
  menu_init();

});

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  if (window.getCount() === 0) {
    application_init();
  }
});
app.on('quit', () => {
  log.info('(main/index) app quit');
  log.info('(main/index) <<<<<<<<<<<<<<<<<<<');
});
global.services = {
  window,
};
global.configs = {
  config,
};
